package com.xzs.demo.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.xzs.demo.entity.UserDao;

/**
 * 自定义拦截器
 *
 */
public class MyInterceptor1 implements HandlerInterceptor {
	/**
	 * 用来存储不拦截的路径
	 */
	private static final String[] IGNORE_URI = { "logins", "demo" };

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		System.out.println(">>>MyInterceptor1>>>>>>>在请求处理之前进行调用（Controller方法调用之前）");
		boolean flag = true;
		String servletPath = request.getServletPath();
		System.out.println("请求路径是: " + servletPath);
		HttpSession session = request.getSession();
		UserDao user = (UserDao) session.getAttribute("user");
		// 检测是否为需要拦截的请求
		for (String s : IGNORE_URI) {
			if (servletPath.contains(s)) {
				if (servletPath.contains("/logins/log")) {
					System.out.println("该请求不需要拦截");
					flag = true;
				} else {
					if (user == null) {
						System.out.println("未登录:" + user);
						response.sendRedirect(request.getContextPath() + "/logins/log");
						flag = false;
					} else {
						System.out.println("web的不需要拦截");
						// session.setAttribute("user", user);
						flag = true;// 只有返回true才会继续向下执行，返回false取消当前请求
					}
				}
				// System.out.println("该请求不需要拦截");
				// flag = true;
			}

		}

		// 需要拦截处理的请求
		// if (!flag) {
		// if (user == null) {
		// System.out.println("未登录:" + user);
		// response.sendRedirect(request.getContextPath() + "/logins/log");
		// flag = false;
		// } else {
		// //session.setAttribute("user", user);
		// flag = true;// 只有返回true才会继续向下执行，返回false取消当前请求
		// }
		// }
		return flag;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		System.out.println(">>>MyInterceptor1>>>>>>>请求处理之后进行调用，但是在视图被渲染之前（Controller方法调用之后）");
		String servletPath = request.getServletPath();
		if (servletPath.contains("web")) {
			System.out.println("web请求路径是: " + servletPath);
			String ipAddr = getIpAddr(request);
			System.out.println("访问者ip是："+ipAddr);
		}

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		System.out.println(">>>MyInterceptor1>>>>>>>在整个请求结束之后被调用，也就是在DispatcherServlet 渲染了对应的视图之后执行（主要是用于进行资源清理工作）");
	}
	private String getIpAddr(HttpServletRequest request) {  
	    String ip = request.getHeader("x-forwarded-for");  
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getHeader("Proxy-Client-IP");  
	    }  
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getHeader("WL-Proxy-Client-IP");  
	    }  
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getRemoteAddr();  
	    }  
	    return ip;  
	}  

}